import javax.swing.*;
import java.awt.event.*;


public class AdminLogin extends JFrame{

    public static void memberLoggingIn(){
        Login frameTable = new Login();
    }

    JButton blogin = new JButton("Login");
    JPanel panel = new JPanel();
    JTextField txuser = new JTextField(15);
    JPasswordField pass = new JPasswordField(15);
    JLabel labelUser = new JLabel( "Admin Username", JLabel.LEFT );
    JLabel labelPassword = new JLabel( "Admin Password", JLabel.LEFT );



    AdminLogin(){
        super("Login Authentification");
        setSize(400,175);
        setLocation(500,280);
        panel.setLayout (null);

        txuser.setBounds(200,30,150,20);
        pass.setBounds(200,65,150,20);
        blogin.setBounds(140,100,80,20);
        labelUser.setBounds(25,30,150,20);
        labelPassword.setBounds(25,65,150,20);

        panel.add(blogin);
        panel.add(txuser);
        panel.add(pass);
        panel.add( labelUser );
        panel.add( labelPassword );

        getContentPane().add(panel);

        setVisible(true);
        actionlogin();
    }

    public void actionlogin(){

        blogin.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent ae ){

                String userName = txuser.getText();
                String userPassword = pass.getText();
                String dbUserResult = "";
                String dbUserPWResult = "";

                try{
                    dbUserResult = DBConnection.getQuery( "select adminname from adminusers where adminname = " + "'" + userName + "'", ImageInJFrame.theConnection );
                    dbUserPWResult = DBConnection.getQuery( "select adminpassword from adminusers where adminpassword = " + "'" + userPassword + "'", ImageInJFrame.theConnection );
                } catch( Exception e ) {
                };

                if( userName.equals( dbUserResult ) && userPassword.equals( dbUserPWResult ) ) {
                    AdminFrame adminLog = new AdminFrame();
                    dispose();
                } else if( userName.equals( dbUserResult ) == false || userPassword.equals( dbUserPWResult ) == false ) {
                    JOptionPane.showMessageDialog( null,"Wrong Username or Password" );
                    txuser.setText("");
                    pass.setText("");
                    txuser.requestFocus();
                }else if( userName.equals( "" ) == false || userPassword.equals( "" ) == false ) {
                    JOptionPane.showMessageDialog( null,"Please enter Username or Password" );
                    txuser.setText("");
                    pass.setText("");
                    txuser.requestFocus();
                }
            }
        });
    }
}

